﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PostSharp4ViewState.Tests
{
   public class TestWithOverrides : BaseControl
   {
      [Persist(Mode = PersistMode.ViewState)]
      public string StringValue;

      [Persist(Mode = PersistMode.ViewState)]
      public int IntValue;

      [Persist(Mode = PersistMode.ControlState)]
      public string StringCSValue;

      [Persist(Mode = PersistMode.ControlState)]
      public int IntCSValue;

      private int a;

      protected override void LoadControlState(object state)
      {
         a = (int)state;
      }
      protected override object SaveControlState()
      {
         return a;
      }
      protected override void LoadViewState(object state)
      {
         base.LoadViewState(state);
      }
      protected override object SaveViewState()
      {
         int a = 17;
         return a;
      }
   }
   [TestClass]
   public class TestWithOverridesTest
   {
      [TestMethod]
      public void ExistingLoadControlStateMatchesSaveControlState()
      {
         TestWithOverrides o = new TestWithOverrides();
         o.StringCSValue = "value";
         o.IntCSValue = 16;
         TestWithoutOverrides newO = new TestWithoutOverrides();
         newO.DoLoadControlState(o.DoSaveControlState());

         Assert.AreEqual("value", newO.StringCSValue);
         Assert.AreEqual(16, newO.IntCSValue);
      }
      [TestMethod]
      public void ExistingLoadControlStateWorks()
      {
         TestWithOverrides o = new TestWithOverrides();
         o.DoLoadControlState(new object[] { 16, new object[] { 4, "value"  }});
         Assert.AreEqual("value", o.StringCSValue);
         Assert.AreEqual(4, o.IntCSValue);
      }
      [TestMethod]
      public void ExistingSaveControlStateWorks()
      {
         TestWithOverrides o = new TestWithOverrides();
         o.IntCSValue = 4;
         o.StringCSValue = "value";
         object[] state = (object[])o.DoSaveControlState();
         Assert.AreEqual(0, state[0]);
         Assert.IsInstanceOfType(state[1], typeof(object[]));
         state = (object[])state[1];
         Assert.AreEqual(state[0], 4);
         Assert.AreEqual(state[1], "value");
      }

      [TestMethod]
      public void ExistingLoadViewStateWorks()
      {
         TestWithOverrides o = new TestWithOverrides();
         o.DoLoadViewState(new object[] { null, new object[] {4, "value" }});
         Assert.AreEqual("value", o.StringValue);
         Assert.AreEqual(4, o.IntValue);
      }
      [TestMethod]
      public void ExistingSaveViewStateWorks()
      {
         TestWithOverrides o = new TestWithOverrides();
         o.IntValue = 4;
         o.StringValue = "value";
         object[] state = (object[])o.DoSaveViewState();
         Assert.AreEqual(17, state[0]);
         Assert.IsInstanceOfType(state[1], typeof(object[]));
         state = (object[])state[1];
         Assert.AreEqual(state[0], 4);
         Assert.AreEqual(state[1], "value");
      }
      public void LoadViewState(object state)
      {
         object[] tmp0 = (object[])state;
         object[] tmp1 = (object[])tmp0[1];
      }
   }
}
